<template>
	<view class="page">
		<view class="page-number">
			<view class="setting-password">
				<view class="inpu-password">
					<text class="text">真实姓名</text>
					<input v-model="name" v-if="isReal==0" placeholder="请输入真实姓名" />
					<input v-model="name" v-else disabled="disabled" :placeholder="userInfo.name" />
				</view>
				<view class="inpu-password">
					<text class="text">身份证号</text>
					<input v-model="card" v-if="isReal==0" placeholder="请输入您的18位身份证号" />
					<input v-model="card" v-else disabled="disabled" :placeholder="userInfo.id_card" />
				</view>
				<view class="upload-image">
					<Rboy-upload-sfz :obverse-url="formData.obverseUrl" :reverse-url="formData.reverseUrl" @selectChange="sfz_chagne" @del="del_btn"></Rboy-upload-sfz>
				</view>
				<view class="btn" @click="onClick" v-if="isReal==0">
					<text>确认</text>
				</view>
				<view class="btn" v-else-if="isReal==1">
					<text>待审核</text>
				</view>
				<view class="btn" v-else-if="isReal==2">
					<text>已认证</text>
				</view>
			</view>
		</view>
	</view>
</template>

<script>
	var _self,loginRes;
	import RboyUploadSfz from "@/components/Rboy-upload-sfz/Rboy-upload-sfz.vue"
	export default {
		components: { RboyUploadSfz },
		data() {
			return {
				userInfo:'',
				isReal: false,
				name: '',
				card: '',
				formData: {
					// 正面
					obverseUrl: "",
					// 反面
					reverseUrl: "",
				},
			};
		},
		onLoad() {
			loginRes = this.checkLogin();
			if(!loginRes)return;
			this.token = loginRes[2];

			this.getUserInfo();
			//console.log(this.confData);
		},
		methods:{
			async getUserInfo(){
				uni.request({
					url: this.apiServer+'/apicom/member/userInfo',
					header: {'content-type' : "application/x-www-form-urlencoded"},
					method: 'POST',
					timeout: 5000,
					data:{
						token : this.token,
					},
					success: res => {
						if(res.data.status == 1){
							var userInfo = res.data.data;
							this.userInfo = userInfo; 
							if(userInfo.id_card=='' && userInfo.name==''){
								this.isReal = 0;//未认证
							}
							if(userInfo.id_card!='' && userInfo.name!='' && userInfo.id_auth==0){
								this.isReal = 1;//待审核
							}
							if(userInfo.id_card!='' && userInfo.name!='' && userInfo.id_auth==1){
								this.isReal = 2;//已认证
							}
						}
					}
				});
			},
			async onClick(){
				if(!this.name){
					uni.showToast({title: '请输入真实姓名',icon: 'none'});
					return;
				}
				if(!this.card){
					uni.showToast({title: '请输入您的18位身份证号',icon: 'none'});
					return;
				}
				if(this.card.length != 18){
					uni.showToast({title: '身份证号码有误',icon: 'none'});
					return;
				}
				if(!this.formData.obverseUrl){
					uni.showToast({title: '请上传身份证正面照',icon: 'none'});
					return;
				}
				if(!this.formData.reverseUrl){
					uni.showToast({title: '请上传身份证背面照',icon: 'none'});
					return;
				}
				uni.request({
					url: this.apiServer+'/apicom/profile/realname',
					header: {'content-type' : "application/x-www-form-urlencoded"},
					method: 'POST',
					timeout: 5000,
					data:{
						token  : this.token,
						name   : this.name,
						id_card: this.card,
						thumb_z: this.formData.obverseUrl,
						thumb_f: this.formData.reverseUrl,
					},
					success: res => {
						if(res.data.status == 1){
							uni.showToast({title: res.data.message,icon: 'none'});
							setTimeout(() =>{
								uni.navigateBack();
							},2000)
						}else{
							uni.showToast({title: res.data.message,icon: 'none'})
						}
					}
				});
			},
			// 获取上传状态
			upload(file,type){
				uni.uploadFile({
					url: this.apiServer+'/apicom/profile/upload?token='+this.token+'&type='+type,
					fileType:'image',
					filePath: file, 
					success: res => {
						console.log(res)
						if(res.statusCode == 200){
							uni.showToast({title: "上传成功",icon: 'none'});
						}else{
							uni.showToast({title: "上传失败",icon: 'none'})
						}
					}
				});
			},
			// 上传
			sfz_chagne(e) {
			    if(this.isReal==1) {
					uni.showToast({title: "正在审核中···",icon: 'none'});
					return;
				}
				if(this.isReal==2) {
					uni.showToast({title: "已认证！",icon: 'none'});
					return;
				}
				if (e.name == "obverse") {
			        this.formData.obverseUrl = e.tempFilePath;
					this.upload(this.formData.obverseUrl,'obverse');
			    } else if (e.name == "reverse") {
			        this.formData.reverseUrl = e.tempFilePath
					this.upload(this.formData.reverseUrl,'reverse');
			    }
			},
			// 删除
			del_btn(e) {
			    if (e.name == "obverse") {
			        this.formData.obverseUrl = ""
			    } else if (e.name == "reverse") {
			        this.formData.reverseUrl = ""
			    }
			},
		}
	}
</script>

<style scoped lang="scss">
	@import 'realName.scss';
</style>
